$(function(){
	
	$("#popup").bind('mouseleave', function(e){
			$("#popup").hide(500);
		});
		
	$("#popup_close").bind('click', function(e){
			$("#popup").hide(500);
		});

	$('.item').each(function(i, item) {

		$(item).bind('mouseenter', function(e){
			var cell=$(this).children('.item_label');
			cell.show(200);
		});

		$(item).bind('mouseleave', function(e){
			var cell=$(this).children('.item_label');
			cell.hide(200);
		});
		
	

		$($(this).children('.item_image')).bind('click', function(e){
			var t = $(this).prev('.item_label').attr('id').split('_');
			var item_id = parseInt(t[1]);
			document.location.href = '/item/'+item_id+'/';
		});


		$('.item_label').bind('click', function(e){
			var t = $(this).attr('id').split('_');
			var item_id = parseInt(t[1]);
			if (!item_id) return false;

			$(this).next('.item_image').css({border:'1px #333 solid'});
			var item_label = $(this);
			var item_image = item_label.next();
			var item_emotion = item_label.children('.item_emotion').attr('value');
			
			if ($("#popup_item_emotion").html() == item_emotion
				&& $("#popup_item_emotion").is(":visible"))
			{
				return false;
			}
			
			$("#popup").slideDown(200);

			var offset = item_label.offset();
			item_label.hide();
			
			$("#popup_i_yes").click(function(){
				$.post('/port/item/'+item_id+'/replyrate/',
					{rate:"yes", reply:$('#popup_reply').attr('value')},
					function(data){
						$.jGrowl(data.result?'评论成功！':'评论失败');
						if (data.code == 2)
						{
							$.jGrowl('请勿对自己发表评论');
						}
						$("#popup").hide(500);
						$('#popup_reply').attr('value', '');
					}, "json");
			});
			
			$("#popup_i_no").click(function(){
				$.post('/port/item/'+item_id+'/replyrate/',
					{rate:"no", reply:$('#popup_reply').attr('value')},
					function(data){
						$.jGrowl(data.result?'评论成功！':'评论失败');
						if (data.code == 2)
						{
							$.jGrowl('请勿对自己发表评论');
						}
						$("#popup").hide(500);
						$('#popup_reply').attr('value', '');
					}, "json");
			});

			$("#popup_item_emotion").html(item_emotion);
			$("#popup_user_data").html('<img src=/images/loading.gif />');
			$("#popup_item_user").html('<img src=/images/loading.gif />');
			$("#popup_item_reply_info").html('<img src=/images/loading.gif />');
			$("#popup_image").attr('src', '/images/loading.gif');
			
			$.post('/port/info/item/'+item_id+'/', function(data) {
				if (data.item_user_id == $.cookie('user_id'))
				{
					$("#popup_reply_area").hide();
				}
				else
				{
					$("#popup_reply_area").show();
				}
				
				var user_url = '/user/'+data.item_user_id+'/';
				$("#popup_item_user").html('<img src="/images/itemadd.png" height=24 /> <a href="'+user_url+'">'+data.item_user_nickname+'</a> @ '+data.create_time);
				$("#popup_item_reply_info").html(data.item_reply_count+'评 '+data.item_yes_count+'喜欢 '+data.item_no_count+'不喜欢');
				$("#popup_image").attr('src', data.item_image_m);
				$("#popup_image").error(function(){
					$(this).attr('src', '/images/no_image_avail.jpg');
				});
				
				setTimeout(function(){
					var win_width = $(window).width(); 
					var win_height = $(window).height(); 
					var pop_offset = $("#popup").offset();
					var pop_left = pop_offset.left;
					var pop_top = pop_offset.top;
					var pop_width = $("#popup").width();
					var pop_height = $("#popup").height();
					
					if (pop_left+pop_width > win_width)
					{
						$("#popup").css({position:'absolute',
							left:win_width-pop_width-10});
					}
					
					//alert($(document).scrollTop());
					if (pop_top+pop_height-$(document).scrollTop() > win_height)
					{
						$("#popup").css({position:'absolute',
							top:win_height+$(document).scrollTop()-pop_height-10});
					}
				}, 200);
				//alert(win_width);

				
			}, "json");
			
			$("#popup_image").bind('click', function(e){
				document.location.href = '/item/'+item_id+'/';
			});
			
			$("#popup").css({position:'absolute', left:offset.left-5, top:offset.top-5});
		});
	});
	
	var action_result = $.cookie('action_result');
	if (action_result && action_result.length > 0)
	{
		$.jGrowl(action_result);
	}
});